package com.hangzhou.xyj.ui.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

import com.hangzhou.xyj.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;

import java.util.ArrayList;
import java.util.List;

import uk.co.senab.photoview.PhotoView;

/**
 * @author : neo.duan
 * @date : 	 2016/4/3
 * @desc : 图片查看器的Adapter
 */
public class PhotoViewAdapter extends PagerAdapter {
    private static final String TAG = "PhotoViewAdapter";
    private Context mContext;
    private List<String> mUrlList;
    private DisplayImageOptions mOptions;
    private List<PhotoView> mViewList = new ArrayList<>();
    private OnPhotoViewClickListener mClickListener;

    public PhotoViewAdapter(Context context, List<String> urlList) {
        this.mContext = context;
        if (urlList == null) {
            urlList = new ArrayList<>();
        }
        this.mUrlList = urlList;
        init();
    }

    public void setOnPhotoViewClickListener(OnPhotoViewClickListener listener) {
        this.mClickListener = listener;
    }

    /**
     * 初始化显示的option
     */
    private void init() {
        mOptions = new DisplayImageOptions.Builder()
//        .showImageForEmptyUri(R.drawable.ic_launcher)  //加载前显示图片
//        .showImageOnFail(R.drawable.ic_launcher)  //加载失败显示图片
                .resetViewBeforeLoading(true)
                .cacheOnDisc(true)
                .imageScaleType(ImageScaleType.EXACTLY)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .displayer(new FadeInBitmapDisplayer(300))  //显示动画
                .build();
    }

    @Override
    public int getCount() {
        return mUrlList.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
        //保证只保存3个View
        if (mViewList.size() > 3) {
            mViewList.remove(object);
        }
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
//        View itemView = View.inflate(mContext,R.layout.layout_photo_view_item,null);
//        ImageView photoView = (ImageView) itemView.findViewById(R.id.iv_photo_view);
//        ImageLoader.getInstance().displayImage(mUrlList.get(position), photoView,mOptions);
//
//        container.addView(itemView);
//        return itemView;

        PhotoView itemView;
        if (mViewList == null || mViewList.isEmpty()) {
            itemView = (PhotoView) View.inflate(mContext, R.layout.layout_photo_view_item, null);
            mViewList.add(itemView);
        }

        final int viewPosition = position % mViewList.size();
        itemView = mViewList.get(viewPosition);
        //获取的itemView已经装载进去了：要新建一个放进list集合中
        if (itemView.getParent() != null) {
            itemView = (PhotoView) View.inflate(mContext, R.layout.layout_photo_view_item, null);
            mViewList.add(itemView);
        }

//        //单击退出
//        PhotoViewAttacher attacher = new PhotoViewAttacher(itemView);
//        attacher.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
//            @Override
//            public void onPhotoTap(View view, float v, float v1) {
//                Log.d(TAG, "图片单击。。。");
//                if (mClickListener != null) {
//                    mClickListener.onPhotoClick(view);
//                }
//            }
//        });

        ImageLoader.getInstance().displayImage(mUrlList.get(position), itemView, mOptions);
        container.addView(itemView);

        return itemView;
    }

    /**
     * 图片点击监听器
     */
    public interface OnPhotoViewClickListener {
        void onPhotoClick(View view);
    }
}
